package stockblaster.client;

import stockblaster.client.facilities.Facilities;
import stockblaster.gen.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

public class ObjectiveActivity extends Activity {
	// TextView obj1t, obj1, obj2t, obj2, obj3t, obj3, obj4t, obj4, obj5t, obj5;
	// TextView obj6t, obj6;
	final Activity thisAct = ObjectiveActivity.this;
	TextView[] objectives;
	Button backBut;

	LinearLayout objLayout;

	// ScrollView objLayoutBox;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.objective_page);

		backBut = (Button) this.findViewById(R.id.button6);
		backBut.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				onBackPressed();
			}
		});

		/*
		 * objectives[0] = (TextView) this.findViewById(R.id.textView1);
		 * objectives[1] = (TextView) this.findViewById(R.id.textView2);
		 * objectives[2] = (TextView) this.findViewById(R.id.textView3);
		 * objectives[3] = (TextView) this.findViewById(R.id.textView4);
		 * objectives[4] = (TextView) this.findViewById(R.id.textView5);
		 * objectives[5] = (TextView) this.findViewById(R.id.textView6);
		 * objectives[6] = (TextView) this.findViewById(R.id.textView7);
		 * objectives[7] = (TextView) this.findViewById(R.id.textView8);
		 * objectives[8] = (TextView) this.findViewById(R.id.textView9);
		 * objectives[9] = (TextView) this.findViewById(R.id.textView10);
		 * objectives[10] = (TextView) this.findViewById(R.id.textView11);
		 * objectives[11] = (TextView) this.findViewById(R.id.textView12);
		 * objectives[12] = (TextView) this.findViewById(R.id.textView13);
		 * objectives[13] = (TextView) this.findViewById(R.id.textView14);
		 */

		objLayout = (LinearLayout) this.findViewById(R.id.objectiveLayout);
		objLayout.removeAllViews();
		// objLayoutBox = (ScrollView)
		// this.findViewById(R.id.objectiveLayoutBox);

		initView();
		setObjectiveStrikeThrough();
	}

	@Override
	public void onBackPressed() {
		// Class<?> temp = CurrentUser.getFromClass(CurrentUser.OBJECTIVE);
		MethodHelper.switch_activity(this,
				CurrentUser.getFromClass(CurrentUser.OBJECTIVE), false);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		objLayout.removeAllViews();
		initView();
		setObjectiveStrikeThrough();
	}

	private void setObjectiveStrikeThrough() {
		int num = 0;
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			num = CurrentUser.getHelpNum() - 1;
		} else {
			num = CurrentUser.objectiveNumber - 1;
		}
		if (num <= 0)
			return;
		for (int i = num * 2 - 1; i >= 0; --i) {
			objectives[i].setPaintFlags(objectives[i].getPaintFlags()
					| Paint.STRIKE_THRU_TEXT_FLAG);
			objectives[i].setTextColor(Color.GRAY);
		}
	}

	private void initView() {
		int max = 0;
		RelativeLayout layout;
		LayoutParams lp;
		int j = 1;
		int objNum = 1;
		if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
			max = CurrentUser.getHelpNum();
			objectives = new TextView[max*2];
		} else {
			max = CurrentUser.objectiveNumber;
			objectives = new TextView[max*2];
			//this is for income
			layout = new RelativeLayout(this);
			layout.setId(j + 20);
			final String incomeStr = "Monthly Earnings From Hotel: " +
					MethodHelper.twoDecimal(Facilities.getTotalMonthlyIncome());
			final TextView incomeTV = new TextView(this);
			incomeTV.setId(j + 21);
			incomeTV.setText(incomeStr);
			incomeTV.setTextColor(Color.GREEN);
			incomeTV.setTextSize(25);
			layout.setPadding(5, 5, 0, 0);
			lp = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
			layout.addView(incomeTV, lp);
			
			final View v1 = new View(this);
			v1.setBackgroundColor(Color.WHITE);
			v1.setLayoutParams(new LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT, 1));

			objLayout.addView(layout);
			objLayout.addView(v1);
			//income ends
		}
		
		for (int i = 0; i < max * 2; i = i + 2, ++objNum) {
			// set up the layout so we can position them nicely
			layout = new RelativeLayout(this);
			layout.setId(j);

			// text view for "Objective X"
			final String objHead = "Objective " + objNum;
			final TextView tv = new TextView(this);
			tv.setId(j + 1);
			tv.setText(objHead);
			tv.setTextColor(Color.CYAN);
			tv.setTextSize(25);
			layout.setPadding(5, 5, 0, 0);
			lp = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
			layout.addView(tv, lp);

			// text view for content
			final TextView tv1 = new TextView(this);
			tv1.setId(j + 2);
			if (CurrentUser.getUsername().equals("Januardo Jankowski")) {
				tv1.setText(ObjectiveText.objectiveArdo[objNum - 1]);
			} else {
				tv1.setText(CurrentUser.objectiveList.get(objNum - 1)
						.getObjective());
			}
			tv1.setTextColor(Color.WHITE);
			tv1.setTextSize(18);
			layout.setPadding(0, 5, 0, 0);
			lp = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.addRule(RelativeLayout.BELOW, tv.getId());
			lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
			layout.addView(tv1, lp);

			// adding them to array for StrikeOut
			objectives[i] = tv;
			objectives[i + 1] = tv1;

			// this is the divider line
			final View v = new View(this);
			v.setBackgroundColor(Color.WHITE);
			v.setLayoutParams(new LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT, 1));
			
			if (!CurrentUser.getUsername().equals("Januardo Jankowski")) {
				if (!CurrentUser.objectiveList.get(objNum - 1).isCompleted()) {
					layout.setClickable(true);
					layout.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							objDialog();
						}
					});
					
					tv.setClickable(true);
					tv.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							objDialog();
						}
					});
					
					tv1.setClickable(true);
					tv1.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							objDialog();
						}
					});
				}
			}
			
			
			objLayout.addView(layout);
			objLayout.addView(v);
		}
	}
	
	private void objDialog() {
		@SuppressWarnings("unused")
		AlertDialog adialog = new AlertDialog.Builder(ObjectiveActivity.this)
				.setMessage("Are you sure you want to complete this objective? " +
						"This will take away " +
						MethodHelper.twoDecimal(CurrentUser.currentObj.getMoneyGoal()) +
						" from your cash.")
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								// User clicked Yes so do some stuff
								if (!CurrentUser.getUsername().equals("Januardo Jankowski")) {
									if (!CurrentUser.checkObjective(thisAct)) {
										double difference = CurrentUser.currentObj.getMoneyGoal() - CurrentUser.getFund();
										MethodHelper.showAlertBox(thisAct,
												"Requirements not met",
												"You need " + MethodHelper.twoDecimal(difference) +
												" more to complete this objective.");
									} else {
										objLayout.removeAllViews();
										initView();
										setObjectiveStrikeThrough();
									}
								}
							}
						})
				.setNegativeButton("No",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,
									int whichButton) {
								// User clicked No so do some stuff
								
							}
						}).show();
		
	}
}
